unit Unit17; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Buttons, sqldb, Unit1, DbCtrls, db;

type

  { TForm17 }

  TForm17 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Datasource1: TDatasource;
    DBComboBox1: TDBComboBox;
    Edit1: TEdit;
    Edit10: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Label1: TLabel;
    Label10: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    codigo : String;
  end; 

var
  Form17: TForm17; 

implementation

{ TForm17 }

procedure TForm17.FormShow(Sender: TObject);
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
  Edit5.Text := '';
  Edit6.Text := '';
  Edit10.Text := '';

  if codigo <> '' then
  begin
    if SQLQuery1.Active = True then
      SQLQuery1.Close;

    if SQLQuery2.Active = True then
      SQLQuery2.Close;

    SQLQuery2.DataBase := Form1.PQConnection1;
    SQLQuery2.Transaction := Form1.SQLTransaction1;
    SQLQuery2.SQL.Text := 'select riva_descripcion from responsabilidad_iva order by riva_descripcion';
    
    SQLQuery2.Open;
    
    DBComboBox1.DataField := 'riva_descripcion';
    DBComboBox1.Clear;
    
    while not SQLQuery2.EOF do
    begin
      DBComboBox1.Items.Add(SQLQuery2.Fields[0].AsString);
      SQLQuery2.Next;
    end;
    
    SQLQuery1.DataBase := Form1.PQConnection1;
    SQLQuery1.Transaction := Form1.SQLTransaction1;
    SQLQuery1.SQL.Text := 'select * from vw_cliente where cln_cdg = ' + codigo;

    SQLQuery1.Open;

    while not SQLQuery1.EOF do
    begin
      Edit1.Text := SQLQuery1.Fields[1].AsString;
      Edit2.Text := SQLQuery1.Fields[2].AsString;
      DBComboBox1.Text := SQLQuery1.Fields[3].AsString;
      Edit3.Text := SQLQuery1.Fields[4].AsString;
      Edit4.Text := SQLQuery1.Fields[6].AsString;
      Edit5.Text := SQLQuery1.Fields[7].AsString;
      Edit6.Text := SQLQuery1.Fields[5].AsString;
      SQLQuery1.Next;
    end;

    Edit10.Text := codigo;

    SQLQuery1.Close;
  end
  else
    Close;
end;

procedure TForm17.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
    
  if SQLQuery2.Active = True then
    SQLQuery2.Close;
end;

procedure TForm17.Button1Click(Sender: TObject);
var
  QueryString : String;

begin
  if (Edit1.Text <> '') and (Edit2.Text <> '') then
  begin
    QueryString := 'update cliente set cln_nombre = ''' + Edit1.Text + ''', cln_cuit = '''
      + Edit2.Text + ''', cln_iva = (select riva_cdg from responsabilidad_iva where '
      + 'riva_descripcion = ''' + DBComboBox1.Text + '''), cln_direccion = ''' + Edit3.Text
      + ''', cln_email = ''' + Edit4.Text + ''', cln_telefono = ''' + Edit5.Text
      + ''', cln_region = ''' + Edit6.Text + ''' where cln_cdg = ' + Edit10.Text;

    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('MODIFICACIÓN EXITOSA');
    Close;
  end
  else
    ShowMessage('ERROR EN EL INGRESO DE DATOS');
end;

procedure TForm17.Button2Click(Sender: TObject);
begin
  Close;
end;

initialization
  {$I unit17.lrs}

end.

